package com.pg.agent.engine.service.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pg.agent.engine.client.vo.HttpParam;
import com.pg.base.enums.ResultEnum;
import com.pg.base.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSources;

@Slf4j
public class ExecuteSSEUtil {

    public static void executeSSE(String url, SSEListener eventSourceListener, String chatGlm) {
        RequestBody formBody = RequestBody.create(chatGlm, MediaType.parse("application/json; charset=utf-8"));
        Request.Builder requestBuilder = new Request.Builder();
        Request request = requestBuilder.url(url).post(formBody).build();
        EventSource.Factory factory = EventSources.createFactory(OkHttpUtil.getInstance());
        //创建事件
        factory.newEventSource(request, eventSourceListener);
        try {
            eventSourceListener.getCountDownLatch().await();
        } catch (InterruptedException e) {
            log.info("Interrupted!", e);
            throw new BizException(ResultEnum.ERROR, e);
        }
    }

    public static JSONObject executeComponentSSE(String url, ComponentSSEListener eventSourceListener, HttpParam httpParam) {

        try {
            RequestBody formBody = RequestBody.create(JSON.toJSONString(httpParam.getBody()), MediaType.parse("application/json; charset=utf-8"));
            Request.Builder requestBuilder = new Request.Builder();
            Request request = requestBuilder.url(url).post(formBody).build();
            EventSource.Factory factory = EventSources.createFactory(OkHttpUtil.getInstance());
            //创建事件
            factory.newEventSource(request, eventSourceListener);
            eventSourceListener.getCountDownLatch().await();

            if (eventSourceListener.getCode() == 1000){
                return eventSourceListener.getJsonValue();
            }

            if (eventSourceListener.getCode()!=0) {
                throw new BizException(1000, eventSourceListener.getMsg());
            }
            return eventSourceListener.getJsonValue();
        } catch (InterruptedException e) {
            log.info("Interrupted!", e);
            throw new BizException(1000,"流式组件执行出错");
        }
    }

    public static void executeConvertComponentSSE(String url, ConvertSSEListener eventSourceListener, HttpParam httpParam) {
        RequestBody formBody = RequestBody.create(JSON.toJSONString(httpParam.getBody()), MediaType.parse("application/json; charset=utf-8"));
        Request.Builder requestBuilder = new Request.Builder();
        Request request = requestBuilder.url(url).post(formBody).build();
        EventSource.Factory factory = EventSources.createFactory(OkHttpUtil.getInstance());
        //创建事件
        factory.newEventSource(request, eventSourceListener);
        try {
            eventSourceListener.getCountDownLatch().await();
        } catch (InterruptedException e) {
            log.info("Interrupted!", e);
            throw new BizException(1000,"流式组件执行出错"+e.getMessage());
        }
    }


}


